package org.zn.etl.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zn.etl.config.DevDataSourceInfo;
import org.zn.etl.datasource.entity.DataSourceInfo;
import org.zn.etl.job.entity.DataMapping;
import org.zn.etl.job.entity.EtlJob;
import org.zn.etl.job.entity.JobAnalyseForm;
import org.zn.etl.misc.DocHtmlBuilder;
import org.zn.etl.misc.EtlTablesDataMapping;
import org.zn.etl.service.EtlAnalyseService;
import org.zn.etl.service.EtlDevService;

@RestController
@RequestMapping("etl/analyes")
public class EtlAnalyseController {
	
	@Autowired
	private EtlAnalyseService etlService;
	
	@Autowired
	private DevDataSourceInfo devDataSourceInfo;
	
	@Autowired
	private EtlDevService etlDevService;
	
	/**
	 * 调试用
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("test/column")
	public String analyseTableColumn() throws Exception {
		
		EtlJob job=new EtlJob();
		
		DataSourceInfo source= devDataSourceInfo.getDataSourceInfo("orcl1");
		DataSourceInfo target= devDataSourceInfo.getDataSourceInfo("shardingSphereDataSource");
		
		job.setSource(source);
		job.setTarget(target);
		
		List<DataMapping> dmlist=	EtlTablesDataMapping.loadTables();
		job.setDataMappings(dmlist);
		
		etlService.tableColumnMapping(job,false);
		
		String resHtml= DocHtmlBuilder.wappedBody(DocHtmlBuilder.bulidTables(job),job.getJobName());
		 
		return resHtml;

	}
	
	/**
	 * 表关系临时对应文档生成
	 * @param srcds
	 * @param tagds
	 * @param schema
	 * @param tables
	 * @return
	 * @throws Exception
	 */
	@GetMapping("table/ref")
	public String analyseTableRef(String srcds, String tagds, String schema ,String tables) throws Exception {
		
		String resHtml=etlService.analyseTableRef(srcds, tagds, schema,tables);
		 
		return resHtml;

	}
	
	/**
	 * 测试
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/test/sharding")
	public String testSharding() throws Exception {
		
		etlDevService.testSharingInsert();
	
		return "success";

	}
	
	/**
	 * 按任务 列对应生成
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/job/colunm")
	public Integer analyseJob(@RequestBody JobAnalyseForm form) throws Exception {
		
		Integer r= etlService.analyseJob(form);
	
		return r;

	}
	
	/**
	 * 按组 文档生成
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/doc/group")
	public String jobGroupDoc(String name ) throws Exception {
	
		return etlService.jobGroupDoc(name);

	}
	

}
